<template>
  <div class="editEenter">
    <TipHead :title="title"></TipHead>

    <el-form
      :model="data"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      label-position="left"
    >
      <el-form-item label="法规名称" prop="title">
        <el-input v-model="data.title"></el-input>
      </el-form-item>
      <el-form-item label="适用区域" prop="scope">
        <el-input v-model="data.scope"></el-input>
      </el-form-item>
      <el-form-item label="法规分类" prop="name">
        <el-input v-model="data.type"></el-input>
      </el-form-item>

      <el-form-item label="发布来源" prop="from">
        <el-input v-model="data.from"></el-input>
      </el-form-item>

      <el-form-item label="发布时间" prop="publishTime">
        <el-date-picker
          v-if="data.effectiveTime"
          type="date"
          placeholder="选择日期"
          v-model="data.publishTime"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="生效时间" prop="effectiveTime">
        <el-date-picker
          v-if="data.effectiveTime"
          type="date"
          placeholder="选择日期"
          v-model="data.effectiveTime"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
    </el-form>

    <TipHead title="详细"></TipHead>
    <p
      v-for="item in data.info"
      :key="item.id"
      :class="{ line: true, isChecked: item.isSelect || item.status == 2 }"
      style="margin-bottom: 20px"
      @click="comCheckChange(item)"
    >
      <Comcheck
        :checked="item.isSelect || item.status == 2"
        :disabled="data.status != 0"
        style="margin-right: 23px"
      ></Comcheck>
      <span v-html="item.content.join('<br/>')"> </span>
    </p>
    <TipHead title="附件" :isRight="true">
      <label for="upload">
        <div style="margin-right: 10px" class="el-icon-plus"></div>
      </label>
      <input
        @change="checkChange"
        multiple
        hidden="true"
        id="upload"
        type="file"
      />
    </TipHead>
    <div class="accessoryClass" :key="index" v-for="(item, index) in base64Arr">
      <a target="_blank" :href="item.url">{{ item.name }}</a>
      <span class="el-icon-close" @click="deleteAccessory(item)"></span>
    </div>
  </div>
</template>

<script>
import TipHead from "./compoents/tipHead.vue";
import Comcheck from "../../../components/Comcheck.vue";

export default {
  components: { TipHead, Comcheck },
  props: {
    data: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      base64Arr: [],
      checked: "",
      isChecked: false,
      // 当前标题
      title: "",

      rules: {
        title: [{ required: true, message: "请输入法律名称", trigger: "blur" }],
        scope: [{ required: true, message: "请输入范围", trigger: "blur" }],
        from: [{ required: true, message: "请输入来源", trigger: "blur" }],
        publishTime: [
          {
            type: "date",
            required: true,
            message: "请选择发布日期",
            trigger: "change",
          },
        ],
        effectiveTime: [
          {
            type: "date",
            required: true,
            message: "请选择生效时间",
            trigger: "change",
          },
        ],
      },
    };
  },
  watch: {
    data(Object) {
      this.title = Object.title;
      this.base64Arr = this.data.files;
    },
  },
  filters: {
    filterContent(data) {
      console.log(data);
      return data.join("<br/>");
    },
  },

  methods: {
    getBase64Fn() {
      return this.base64Arr;
    },
    // 附件
    deleteAccessory(file) {
      this.base64Arr = this.base64Arr.filter((item) => {
        return item.id != file.id;
      });
      console.log(this.base64Arr);
    },
    changeto64(file) {
      console.log(file, "wa");
      let reader = new FileReader();
      reader.addEventListener(
        "load",
        () => {
          this.base64Arr.push({
            base64: reader.result,
            name: file.name,
            url: URL.createObjectURL(file),
            id: file.lastModified,
          });
        },
        false
      );

      if (file) {
        reader.readAsDataURL(file);
        console.log(this.base64Arr);
      }
    },
    checkChange({ target }) {
      let files = target.files;
      for (let index = 0; index < files.length; index++) {
        this.changeto64(files[index]);
      }
    },
    // 添加
    add() {},
    // 选择框改变时
    comCheckChange(item) {
      if (this.data.status != 0) {
        return;
      }
      this.$set(item, "isSelect", !item.isSelect);

      // console.log(item, "只想做你的太阳");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.editEenter {
  .accessoryClass {
    display: flex;
    padding: 20px;
    justify-content: space-between;
    border: 1px solid black;
    margin-bottom: 10px;
  }
  ::v-deep .el-form {
    width: 100%;
    display: grid;
    grid-row-gap: 20px;
    grid-template-columns: 1fr 1fr;
  }
  .el-form-item {
    margin-top: 0px;
  }
  .line {
    // justify-content: space-between;
    display: flex;
    align-items: center;
  }
  .isChecked {
    color: #e9ebec;
  }
}
</style>